package domain.service.charge;

import domain.dao.charge.ChargeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Administrator on 2017/9/6.
 */
@Service
public class ChargeServices {
    @Autowired
    private ChargeDao chargeDao;

    /**
     * 获取月度投资数据
     *
     * @param year
     */
    public Map<String, Object> getMonthMoney(String year) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> resultData = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> data = chargeDao.getEveryMouthData(year);
        if (data != null && data.size() > 0) {
            for (int i = 1; i < 13; i++) {
                boolean bln = false;
                String month = year + "-" + i;
                for (Map<String, Object> entry : data) {
                    if (month.equals(entry.get("DT"))) {
                        resultData.add(entry);
                        bln = true;
                        break;
                    }
                }
                if (!bln) {
                    Map<String, Object> entry = new HashMap<String, Object>();
                    entry.put("DT", month);
                    entry.put("DATA1", 0.0d);
                    entry.put("DATA2", 0.0d);
                    entry.put("DATA3", 0.0d);
                    entry.put("THIS_MONTH", 0.0d);
                    resultData.add(entry);
                }
            }
        }
        splicing_data(year, resultMap, resultData);
        return resultMap;
    }

    private void splicing_data(String year, Map<String, Object> resultMap, List<Map<String, Object>> resultData) {
        Calendar now = Calendar.getInstance();
        String nowYear = now.get(Calendar.YEAR) + "";
        String nowMonth = now.get(Calendar.MONTH) + 2 + "";
        /***保存直接工程数据**/
        List<Double> directMoneyArray = new ArrayList<Double>();
        /***保存动迁数据**/
        List<Double> resettlementMoneyArray = new ArrayList<Double>();
        /***保存其他数据**/
        List<Double> otherMoneyArray = new ArrayList<Double>();
        /*****保存月度累计投资*****/
        List<Double> thisMoneyArray = new ArrayList<Double>();
        /*****保存月度累计投资*****/
        List<Double> sumMoneyArray = new ArrayList<Double>();
        /************直接工程费用月度统计 一标段**********/
        List<Double> oneMoneyArray = new ArrayList<Double>();
        List<Double> twoMoneyArray = new ArrayList<Double>();
        List<Double> threeMoneyArray = new ArrayList<Double>();
        //最终数据接口list
        for (Map<String, Object> entity : resultData) {
            String month = entity.get("DT").toString();
            String monthVal = month.substring(5, month.length());
            if (nowYear.equals(year) && nowMonth.equals(monthVal)) {
                break;
            }
            String sql = "select (a.sumVal+b.sumVal)total from(select NVL(sum(TRUNC(THISMONTHFINISH,5)), 0)sumVal from charge where TO_NUMBER（SUBSTR(dt,0,4)) < " + year + " and typename in('一标段','二标段','三标段','动迁费用','其它费用') and prjname='实际产值')a,(select NVL(sum(TRUNC(THISMONTHFINISH,5)), 0)sumVal from charge where TO_NUMBER（SUBSTR(dt,0,4))= " + year + " and SUBSTR(dt,INSTR(dt,'-',1,1)+1,2)<=" + monthVal + " and typename in('一标段','二标段','三标段','动迁费用','其它费用') and prjname='实际产值')b";
            Double total = chargeDao.executeSql(sql);
            entity.put("total", total + "");
            directMoneyArray.add(Double.parseDouble(entity.get("DATA1").toString()));
            resettlementMoneyArray.add(new BigDecimal(entity.get("DATA2").toString()).doubleValue());
            otherMoneyArray.add(new BigDecimal(entity.get("DATA3").toString()).doubleValue());
            thisMoneyArray.add(new BigDecimal(entity.get("THIS_MONTH").toString()).doubleValue());
            sumMoneyArray.add(new BigDecimal(entity.get("total").toString()).doubleValue());
            /************直接工程费用月度统计**********/
            sql = " select NVL(sum(TRUNC(THISMONTHFINISH,5)), 0) from charge where  dt = '" + month + "' and typename = '一标段' and prjname='实际产值' ";
            Double oneMoney = chargeDao.executeSql(sql);
            oneMoneyArray.add(oneMoney);
            sql = " select NVL(sum(TRUNC(THISMONTHFINISH,5)), 0) from charge where  dt = '" + month + "' and typename = '二标段' and prjname='实际产值' ";
            Double twoMoney = chargeDao.executeSql(sql);
            twoMoneyArray.add(twoMoney);
            sql = " select NVL(sum(TRUNC(THISMONTHFINISH,5)), 0) from charge where  dt = '" + month + "' and typename = '三标段' and prjname='实际产值' ";
            Double threeMoney = chargeDao.executeSql(sql);
            threeMoneyArray.add(threeMoney);
        }
        resultMap.put("data1", directMoneyArray);
        resultMap.put("data2", resettlementMoneyArray);
        resultMap.put("data3", otherMoneyArray);
        resultMap.put("data4", sumMoneyArray);
        resultMap.put("data5", thisMoneyArray);
        resultMap.put("oneMoneyArray", oneMoneyArray);
        resultMap.put("twoMoneyArray", twoMoneyArray);
        resultMap.put("threeMoneyArray", threeMoneyArray);
    }

    public Map<String, Object> getMolecular() {
       return chargeDao.getMolecular();
    }
}
